package dev.ece.suit.hexmap;

import java.util.ArrayList;
import java.util.List;

import dev.ece.core.texture.Texture;
import dev.ece.suit.hexmap.terrain.TerrainRenderer;
import dev.ece.util.lib.Vector2f;
import dev.ece.util.lib.Vector3f;
import dev.ece.util.tools.Direction;

/**
 * 三角接缝
 * @author 潜行的虚影
 *
 */
@Deprecated
public class HexMapTriangle {
	
	private HexMapCell[] cells = new HexMapCell[3];
	
	private final List<Vector3f> vertice = new ArrayList<Vector3f>();
	
	private final List<Vector2f> uv = new ArrayList<Vector2f>();
	
	private final List<Vector3f> normals = new ArrayList<Vector3f>();
	
	private Vector2f[] alpha;
	
	private TerrainRenderer renderer;
	
	/**
	 * 方向，0 上，1 下
	 */
	private int director;
	
	private int first;
	
	private HexMapTriangle() {}
	
	public void setup() {
		renderer.setup();
		
		uv.clear();
		normals.clear();
		alpha = null;
	}

	public void render(HexMapTriangleShader hexMapTriangleShader, Texture[] textures) {
		hexMapTriangleShader.glMaterial(
				cells[0].getTerrainType().getMaterial(), 
				cells[1].getTerrainType().getMaterial(),
				cells[2].getTerrainType().getMaterial());
		hexMapTriangleShader.glTexture(
				textures[cells[0].getTerrainType().getTextureIndex()], 
				textures[cells[1].getTerrainType().getTextureIndex()],
				textures[cells[2].getTerrainType().getTextureIndex()]);
		renderDepthMap();
	}
	
	public void renderDepthMap() {
		renderer.render(this.getFirst());
	}

	public boolean isIntersected(Vector3f o, Vector3f dir, HexMapPickUp hexMapPickUp) {
		boolean isIntersected = renderer.isIntersected(o, dir, vertice, hexMapPickUp.getPoint());
		if(isIntersected) {
			hexMapPickUp.setTriangle(this);
			boolean hasWater = this.cells[0].getTerrainType().hasWater() || 
					this.cells[1].getTerrainType().hasWater() || 
					this.cells[2].getTerrainType().hasWater();
			if(hasWater) {
				//水面高度0
				hexMapPickUp.getPoint().setY(Math.max(hexMapPickUp.getPoint().getY(), 0));
			}
		}
		return isIntersected;
	}
	
	/**
	 * @param renderer 渲染器
	 * @param cells
	 * @param alpha 混合度
	 * @param row
	 * @param col
	 * @param director 方向，0 上，1 下
	 * @return
	 */
	public static HexMapTriangle createHexMapTriangle(TerrainRenderer renderer, HexMapCell[][] cells, Vector2f[] alpha, int row, int col, int director) {
		int rows = cells.length;
		int cols = cells[0].length;
		HexMapCellIndex thirdIndex = HexMapCell.getHexMapCell(rows, cols, row, col, director == 1 ? Direction.DIRECTION_2 : Direction.DIRECTION_0);
		if(thirdIndex == null) {
			return null;
		}
		HexMapCellIndex rightIndex = HexMapCell.getHexMapCell(rows, cols, row, col, Direction.DIRECTION_1);
		if(rightIndex == null) {
			return null;
		}
		HexMapTriangle triangle = new HexMapTriangle();
		triangle.alpha = alpha;
		triangle.renderer = renderer;
		triangle.director = director;
		triangle.cells[0] = cells[row][col];
		if(director == 1) {
			triangle.cells[1] = thirdIndex.getHexMapCell(cells);
			triangle.cells[2] = rightIndex.getHexMapCell(cells);
		} else {
			triangle.cells[1] = rightIndex.getHexMapCell(cells);
			triangle.cells[2] = thirdIndex.getHexMapCell(cells);
		}
		return triangle;
	}
	
	public HexMapCell[] getCells() {
		return cells;
	}
	
	public List<Vector3f> getVertice() {
		return vertice;
	}

	public List<Vector2f> getUv() {
		return uv;
	}

	public List<Vector3f> getNormals() {
		return normals;
	}


	public Vector2f[] getAlpha() {
		return alpha;
	}

	public TerrainRenderer getRenderer() {
		return renderer;
	}

	public int getDirector() {
		return director;
	}
	
	public void destroy() {
		vertice.clear();
	}
	
	protected int getFirst() {
		return first;
	}

	protected void setFirst(int first) {
		this.first = first;
	}

	@Override
	public String toString() {
		StringBuilder builder = new StringBuilder();
		for(Vector3f vec: vertice) {
			builder.append(vec.toString()).append("\n");
		}
		return builder.toString();
	}
}
